package top.javarem.medical.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.javarem.medical.common.response.Response;
import top.javarem.medical.domain.dto.AnswerResultDTO;
import top.javarem.medical.domain.dto.AnswerSubmitDTO;
import top.javarem.medical.domain.dto.QuestionDTO;
import top.javarem.medical.domain.vo.AdminQuestionVO;
import top.javarem.medical.domain.vo.QuestionDetailVO;
import top.javarem.medical.domain.vo.QuestionVO;
import top.javarem.medical.service.QuestionService;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/v1/medical/question")
@CrossOrigin("*")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    //获取问题列表
    @GetMapping("/show/{categoryId}")
    public Response<List<QuestionDTO>> getQuestions(@PathVariable Integer categoryId) {
        if (categoryId == null) {
            return Response.error();
        }
        List<QuestionDTO> questionDTOList = questionService.queryByCategoryId(categoryId);
        return Response.success(questionDTOList);
    }

    //提交答案
    @PostMapping("/answer_submit")
    public Response<AnswerResultDTO> submitAnswer(@RequestBody AnswerSubmitDTO answerSubmitDTO) {

        try {
            if (answerSubmitDTO == null)
                return Response.error();
            AnswerResultDTO answerResultDTO = questionService.checkAnswer(answerSubmitDTO);
            if (answerResultDTO != null) {
                return Response.success(answerResultDTO);
            }
            return Response.error();
        } catch (Exception e) {
            log.error("{}", e);
            return Response.error();
        }
    }

    //分页查询题目（后台管理接口）
    @GetMapping("/page")
    public Response<List<AdminQuestionVO>> pageQuery(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<AdminQuestionVO> page = questionService.pageQuery(categoryId, type,
                    difficulty, pageNum, pageSize);
            return Response.success(page);
        } catch (Exception e) {
            log.error("分页查询题目失败", e);
            return Response.error();
        }
    }
//获取问题详情
    @GetMapping("/query_question/{id}")
    public Response<QuestionDetailVO> getQuestion(@PathVariable Integer id) {

        QuestionDetailVO questionDTO = questionService.queryById(id);
        return Response.success(questionDTO);

    }

    @PostMapping("/add")
    public Response<Boolean> addQuestion(@RequestBody QuestionVO vo) {
        try {
            return Response.success(questionService.addQuestion(vo));
        } catch (Exception e) {
            log.error("添加题目失败", e);
            return Response.error();
        }
    }

    @PutMapping("/update")
    public Response<Boolean> updateQuestion(@RequestBody QuestionVO vo) {
        try {
            return Response.success(questionService.updateQuestion(vo));
        } catch (Exception e) {
            log.error("更新题目失败", e);
            return Response.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    public Response<Boolean> deleteQuestion(@PathVariable Long id) {
        try {
            return Response.success(questionService.deleteQuestion(id));
        } catch (Exception e) {
            log.error("删除题目失败", e);
            return Response.error();
        }
    }

    @PostMapping("/batch/delete")
    public Response<Boolean> batchDelete(@RequestBody List<Long> ids) {
        try {
            return Response.success(questionService.removeBatchByIds(ids));
        } catch (Exception e) {
            log.error("批量删除题目失败", e);
            return Response.error();
        }
    }

}
